package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.service.AlertRuleService;
import com.zzyl.vo.AlertRuleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.zzyl.base.ResponseResult.success;

@RestController
@RequestMapping("/alert-rule")
@Api(tags = "管理端 - 报警规则相关接口")
public class AlertRuleController {

    @Autowired
    private AlertRuleService alertRuleService;

    /**
     * 新增报警规则
     * @param alertRuleDto
     * @return
     */
    @PostMapping("/create")
    @ApiOperation("新增报警规则")
    public ResponseResult createRule(@RequestBody AlertRuleDto alertRuleDto){
        alertRuleService.createRule(alertRuleDto);
        return success();
    }

    /**
     * 分页获取告警规则列表
     * @param pageNum
     * @param pageSize
     * @param alertRuleName
     * @param functionName
     * @param productKey
     * @return
     */
    @GetMapping("/get-page")
    @ApiOperation("分页获取告警规则列表")
    public ResponseResult<PageResponse<AlertRuleVo>> getAlertRulePage(
            @ApiParam(value = "页码", required = true) @RequestParam(value = "pageNum") Integer pageNum,
            @ApiParam(value = "每页大小", required = true) @RequestParam(value = "pageSize") Integer pageSize,
            @ApiParam(value = "规则名称，精确查询") @RequestParam(value = "alertRuleName", required = false) String alertRuleName,
            @ApiParam(value = "功能名称，精确查询") @RequestParam(value = "functionName", required = false) String functionName,
            @ApiParam(value = "产品ID") @RequestParam(value = "productKey", required = false) String productKey
    ){
        PageResponse<AlertRuleVo> pageResponse = alertRuleService.getAlertRulePage(pageNum, pageSize, alertRuleName, functionName, productKey);
        return success(pageResponse);
    }

    /**
     * 获取单个告警规则
     * @param id
     * @return
     */
    @GetMapping("/read/{id}")
    @ApiOperation("获取单个告警规则")
    public ResponseResult<AlertRuleVo> readAlertRule(@PathVariable Long id){
        AlertRuleVo alertRuleVo = alertRuleService.readAlertRule(id);
        return success(alertRuleVo);
    }

    /**
     * 更新告警规则
     * @param alertRuleDto
     * @return
     */
    @PutMapping("/update/{id}")
    @ApiOperation("更新告警规则")
    public ResponseResult updateAlertRule(@RequestBody AlertRuleDto alertRuleDto){
        alertRuleService.updateAlertRule(alertRuleDto);
        return success();
    }

    /**
     * 删除告警规则
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除告警规则")
    public ResponseResult deleteAlertRule(@PathVariable Long id){
        alertRuleService.deleteAlertRule(id);
        return success();
    }

    /**
     * 启用-禁用
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/{id}/status/{status}")
    @ApiOperation("启用-禁用")
    public ResponseResult enableOrDisable(@PathVariable Long id, @PathVariable Integer status){
        alertRuleService.enableOrDisable(id, status);
        return success();
    }
}
